<?php
require_once(APPPATH.'views/components/page.php');
require_once (APPPATH.'libraries/paginationobject.php');
require_once (APPPATH.'libraries/productobject.php');
/**
 * Product Controller
 */
class Product extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('product_model', 'product');
		$this->load->model('branch_model');
	}
	
	public function index($curPage = 0)
	{
		$error = ERROR_NONE;
		if (Page::getInstance()->isAdmin())
		{
			$categories = Category_Model::getInstance()->getStorageCategories();
			$categoryId = $this->session->userdata('categoryid');
			$producttypeId = $this->session->userdata('producttypeid');
			
			if ($categoryId == NULL)
				$categoryId = count($categories) > 0 ? $categories[0]->getId() : 0;
			
			if ($producttypeId == NULL)
				$producttypeId = 0;
			
			$this->session->set_userdata('error', $error);
			$this->session->set_userdata('categoryid', $categoryId);
			$this->session->set_userdata('producttypeid', $producttypeId);
			$this->getProductsByCategoryId($categoryId, 0);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function view($id)
	{
		$product = $this->product->getProductById($id);
		
		if ($product->getId() > 0)
		{
			$productType = $this->producttype_model->getProductTypeById($product->getProductType()->getId());
			$category = $this->category_model->getCategoryById($productType->getCategoryId());
			$data['product'] = $product;
			$data['producttype'] = $productType;
			$data['category'] = $category;
			$data['related_products'] = $this->product->getRandomProducts(RELATED_PRODUCTS, $productType->getId());
		}
		else
			$data['rerror'] = NOT_EXISTED_ERROR;
		
		$this->load->view('products/product_detail', $data);
	}
	
	// Admin
	public function search($key = NULL, $curPage = 0)
	{
		if (Page::getInstance()->isAdmin())
		{
			$rec_limit = REC_LIMIT;
			
			if ($curPage == 0)
			{
				$offset = 0;
				$page = 0;
			}
			else {
				$page = $curPage - 1;
				$offset = $rec_limit * $page;
			}
			
			$total = $this->product->countProductsByName($key);
			$paginationLinks = $this->getPaginationLinks('product/search/'.$key, $total, $curPage, 4);
			
			$products = $this->product->getProductsByName($key, $offset, $rec_limit);
			
			$data['products'] = $products;
			$data['pagination'] = $paginationLinks;
			$this->load->view('products/product_search_admin_view', $data);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function processSearch($curPage = 0)
	{
		$key = $this->input->post('searchtxt');
		redirect('product/search/'.$key);
	}
	
	private function generateProductTable($productTypes, $products, $pagingLinks)
	{
		$data['producttypes'] = $productTypes;
		$data['products'] = $products;
		$data['pagination'] = $pagingLinks;
		
		$this->load->view('products/product_admin_view', $data);
	}
	
	private function getPaginationLinks($url, $total, $curPage, $segment)
	{
		$config = PaginationObject::getInstance()->createPaginationConfig($url, $total, $curPage, $segment);
		$this->pagination->initialize($config);
		
		return $this->pagination->create_links();
	}
	
	public function getProductsByCategoryId($id, $curPage = 0) // AJAX
	{
		if ($id == 0)
		{
			$categories = Category_Model::getInstance()->getStorageCategories();
			$id = count($categories) > 0 ? $categories[0]->getId() : 0;
		}
		
		$rec_limit = REC_LIMIT;
			
		if ($curPage == 0)
		{
			$offset = 0;
			$page = 0;
		}
		else {
			$page = $curPage - 1;
			$offset = $rec_limit * $page;
		}
		
		$total = $this->product->countProductsByCategoryId($id);
		$paginationLinks = $this->getPaginationLinks('product/getProductsByCategoryId/'.$id, $total, $curPage, 4);
		
		$products = $this->product->getProductsByCategoryId($id, $offset, $rec_limit);
		$productTypes = $this->producttype_model->getProductTypesByCategoryId($id);
		
		$this->session->set_userdata('categoryid', $id);
		$this->generateProductTable($productTypes, $products, $paginationLinks);
	}
	
	public function getProductsByProductTypeId($categoryId, $productTypeId = 0, $curPage = 0) // AJAX
	{
		if ($productTypeId == 0)
		{
			$this->getProductsByCategoryId($categoryId);
		}
		else {
			$rec_limit = REC_LIMIT;
			
			if ($curPage == 0)
			{
				$offset = 0;
				$page = 0;
			}
			else {
				$page = $curPage - 1;
				$offset = $rec_limit * $page;
			}
			
			$total = $this->product->countProductsByProductTypeId($productTypeId);
			$paginationLinks = $this->getPaginationLinks('product/getProductsByProductTypeId/'.$categoryId."/".$productTypeId, $total, $curPage, 5);
			
			$products = $this->product->getProductsByProductTypeId($productTypeId, $offset, $rec_limit);
			
			$this->session->set_userdata('categoryid', $categoryId);
			$this->session->set_userdata('producttypeid', $productTypeId);
			
			$productTypes = $this->producttype_model->getProductTypesByCategoryId($categoryId);
			$this->generateProductTable($productTypes, $products, $paginationLinks);
		}
	}
	
	public function delete()
	{
		$error = ERROR_NONE;
		
		if (Page::getInstance()->isAdmin())
		{
			$ids = $this->input->post('products');
			$error = $this->product->deleteMultipleProducts($ids);
		}
		else
		{
			redirect(site_url());
		}
		
		$categoryId = $this->input->post('categoryid');
		$productTypeId = $this->input->post('producttypeid');
		
		$this->session->set_userdata('categoryid', $categoryId);
		$this->session->set_userdata('producttypeid', $productTypeId);
		$this->session->set_userdata('error', $error);
		redirect('product');
	}

	public function insert()
	{
		if (Page::getInstance()->isAdmin())
		{
			$this->load->view('products/product_insert_view');
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function processInsert()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			$name = $this->input->post('productname');
			$productTypeId = $this->input->post('producttypeid');
			$productType = $this->producttype_model->getProductTypeById($productTypeId);
			
			if ($this->product->findProduct($name, $productTypeId) < 0)
			{
				$markids = $_POST['markids'];
				$branchId = $this->input->post('branchid');
				$photo = $this->input->post('phototxt');
				$price = $this->input->post('pricetxt');
				$desc = $this->input->post('description');
				$promotion = $this->input->post('promotion') != NULL ? TRUE : FALSE;
				
				$branch = $this->branch_model->getBranchById($branchId);
				$marks = array();
				
				foreach ($markids as $id)
				{
					$marks[] = SpecialMark_Model::getInstance()->getSpecialMarkById($id);
				}
				
				$product = new ProductObject(-1, $productType, $branch, $desc, $price, $marks, $photo, $name, $promotion);
				
				$error = $this->product->insertProduct($product);
			}
			else
			{
				$error = EXISTED_ERROR;
				$this->session->set_userdata('productname', $name);
				$this->session->set_userdata('producttypename', $productType->getTitle());
			}
			
			$this->session->set_userdata('error', $error);
			
			if ($error == ERROR_NONE)
			{
				redirect('product');
			}
			else
			{
				$this->session->set_userdata('producttypeid', $productTypeId);
				redirect('product/insert');
			}
		}
		else
		{
			redirect(site_url());
		}
		
	}
	
	public function edit($id)
	{
		if (Page::getInstance()->isAdmin())
		{
			$product = $this->product->getProductById($id);
			$data['product'] = $product;
			
			if ($product->getId() <= 0)
				$data['rerror'] = NOT_EXISTED_ERROR;
			
			$this->load->view('products/product_insert_view', $data);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function processUpdate()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			$productId = $this->input->post('productid');
			$product = $this->product->getProductById($productId);
			
			$name = $this->input->post('productname');
			$productTypeId = $this->input->post('producttypeid');
			
			$foundId = $this->product->findProduct($name, $productTypeId);
			$existed = FALSE;
			
			if ($foundId == $productId)
				$existed = FALSE;
			else {
				if ($foundId > 0)
					$existed = TRUE;
			}
		
			if (! $existed)
			{
				$markids = $_POST['markids'];
				$branchId = $this->input->post('branchid');
				$photo = $this->input->post('phototxt');
				$price = $this->input->post('pricetxt');
				$desc = $this->input->post('description');
				$promotion = $this->input->post('promotion') != NULL ? TRUE : FALSE;
				
				$productType = $this->producttype_model->getProductTypeById($productTypeId);
				$branch = $this->branch_model->getBranchById($branchId);
				$marks = array();
				
				foreach ($markids as $id)
				{
					$marks[] = SpecialMark_Model::getInstance()->getSpecialMarkById($id);
				}
				
				$product->setName($name);
				$product->setProductType($productType);
				$product->setMarks($marks);
				$product->setBranch($branch);
				$product->setImage($photo);
				$product->setPrice($price);
				$product->setDescription($desc);
				$product->setPromotion($promotion);
				
				$error = $this->product->updateProduct($productId, $product);
			}
			else {
				$error = EXISTED_ERROR;
				$this->session->set_userdata('productname', $name);
				$this->session->set_userdata('producttypename', $product->getProductType()->getTitle());
			}
			
			$this->session->set_userdata('error', $error);
		
			if ($error == ERROR_NONE)
				redirect('product');
			else
			{
				$this->session->set_userdata('categoryid', $product->getProductType()->getCategoryId());
				$this->session->set_userdata('producttypeid', $product->getProductType()->getId());
				redirect('product/edit/'.$productId);
			}
		}
		else
		{
			redirect(site_url());
		}
	}
}
/* End of file product.php */
/* Location: ./application/controllers/product.php */